//
//  MainScrollViewPage1.m
//  HappyBug
//
//  Created by liulei on 2018/10/31.
//  Copyright © 2018年 www.hc.com.cn. All rights reserved.
//

#import "MainScrollViewPage1.h"
#import "MJRefreshHeader.h"
#import "MJRefreshFooter.h"
//普通下拉刷新
#import "MJRefreshNormalHeader.h"
#define MJRandomData [NSString stringWithFormat:@"%d", arc4random_uniform(1000000)]
#import "CustTableViewCell.h"
static const CGFloat MJDuration = 1.0;

@interface MainScrollViewPage1 ()

@end

@implementation MainScrollViewPage1{
    NSMutableArray  *tabledata;
    NSMutableDictionary *tableDictory;
    CustTableViewCell *customcell;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self initData];
    [self initView];
    
}

- (void)loadNewData
{
    // 1.添加假数据
    for(int i=0;i<15;i++){
        tableDictory =[[NSMutableDictionary alloc]init];
        [tableDictory setObject:[NSString stringWithFormat:@"名字%d",i] forKey:@"titleName"];
        [tableDictory setObject:[NSString stringWithFormat:@"描述信息真的长。。。。%d",i] forKey:@"contentName"];
        [tableDictory setObject:@"img8" forKey:@"imageName"];
        [tabledata insertObject:tableDictory atIndex:0];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        // 2.模拟2秒后刷新表格UI（真实开发中，可以移除这段gcd代码）
        __weak UITableView *tableView = self.tableView;
        // 刷新表格
        [self.tableView reloadData];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.mj_header endRefreshing];
        
    });
}

-(void)initView{
    __weak typeof(self) weakSelf = self;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //使用刷新的事件
    //table.tableHeaderView= [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
    //顶部-使用block下拉刷新数据
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadNewData];
        
        
    }];
    //
    //
    //    //底部部-使用block下拉刷新数据
    //    self.tableView.mj_footer=[MJRefreshFooter footerWithRefreshingBlock:^{
    //
    //        [weakSelf loadNewData];
    //
    //    }];
    //
    //    [self.tableView.mj_footer beginRefreshing];
    
    //tableview 头
    UIView *headview =[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    [headview setBackgroundColor:[UIColor blueColor]];
    
    self.tableView.tableHeaderView=headview;
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return tabledata.count;
    
}

//UILabel *titltLabel;
//@property(nonatomic,strong) UILabel *contentLabel;
//@property(nonatomic,strong) UIImageView *leftImage;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    CustTableViewCell *selectcell=  (CustTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
//
//   selectcell.title
//
//    [tableDictory setObject:[NSString stringWithFormat:@"我是被点击了"] forKey:@"titleName"];
//    [tableDictory setObject:[NSString stringWithFormat:@"描述信息真的长我是被点击了"] forKey:@"contentName"];
//    [tableDictory setObject:@"img9" forKey:@"imageName"];
//
//    [tabledata addObject:tableDictory];
//    [tableView reloadData];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    customcell =[tableView dequeueReusableCellWithIdentifier:@"CUSTOMCELL"];
    
    if(!customcell){
        customcell =[[CustTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CUSTOMCELL"];
    }
    
    NSMutableDictionary *dic=   tabledata[indexPath.row];
    [customcell setTableCellContentWithDic:dic];
    return  customcell;
    
}

-(void)initData{
    tabledata=[[NSMutableArray alloc]init];
    for(int i=0;i<5;i++){
        tableDictory =[[NSMutableDictionary alloc]init];
        [tableDictory setObject:[NSString stringWithFormat:@"名字%d",i] forKey:@"titleName"];
        [tableDictory setObject:[NSString stringWithFormat:@"描述信息真的长。。。。%d",i] forKey:@"contentName"];
        [tableDictory setObject:@"img8" forKey:@"imageName"];
        [tabledata addObject:tableDictory];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
